package cate.game.pvp.ladder;

import cate.common.rpc.path.RpcLadderPath;
import cate.game.GameBody;
import cate.game.framework.GameException;
import cate.game.framework.ModuleMan;
import cate.game.pvp.ladder.common.*;
import cate.game.pvp.ladder.msg.LadderDataResp;
import cate.game.role.Role;
import cp.solution.rpc.RpcMsgHead;
import easy.java.dev.note.NoteClass;

@NoteClass("新天梯赛全服管理")
public class LadderManager extends ModuleMan {

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        // 不依赖其他模块的 初始化方法
    }

    @Override
    public void initDependent() throws Exception {
        // 依赖其他模块的 初始化方法
        game.rpcRecv().onFireAndForgetTemplate(RpcLadderPath.PLAY_REPORT_G2G, LadderReportQuery.class, this::onAddRecordCmd);
        game.rpcRecv().onRequestResponseTemplate(RpcLadderPath.OPPO_GUILD_G2G, LadderLockOppoQuery.class, this::getOppoGuildCmd);
        game.rpcRecv().onFireAndForgetTemplate(RpcLadderPath.RESET_LADDER_C2G, String.class, this::reset);
    }



    public void tick() {
        // 每秒执行，非阻塞的逻辑才能写在这里，耗时要极短
    }

    @Override
    public void shutdown() {
        // 当游戏服被关闭时
    }

    @Override
    public void onDaySpan() {
        // 当跨天时
    }


    /** 获取对手PlaySide, 阻塞方法，慎用 */
    public LadderLoadOppoPsResult loadOpponentPlaySide(String opponentId, boolean robot) throws GameException {
        LadderLoadOppoPsQuery query = new LadderLoadOppoPsQuery();
        query.opponentId = opponentId;
        query.robot = robot;
        LadderLoadOppoPsResult r = game.rpcSend().requestResponseCross(RpcLadderPath.LOAD_OPPO_PS_G2C, query, LadderLoadOppoPsResult.class);
        return r;
    }

    /** 向跨服申请锁定对手 */
    public LadderLockOppoResult lockOpponent(LadderLockOppoQuery query) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.LOCK_OPPO_G2C, query, LadderLockOppoResult.class);
    }

    /** 向跨服申请解锁对手 */
    public LadderLockOppoResult unLockOpponent(LadderLockOppoQuery query) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.UNLOCK_OPPO_G2C, query, LadderLockOppoResult.class);
    }

    /** 向跨服申请获取个人排名 */
    public LadderLoadOppoPsResult getRankById(String id) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.REFRESH_RANK_G2C, id, LadderLoadOppoPsResult.class);
    }

    /** 向跨服申请更新榜单 */
    public LadderRankUpdateResult updateRankList(LadderRankUpdateQuery query) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.UPDATE_RANK_G2C, query,LadderRankUpdateResult.class);
    }

    /** 向跨服申请更新防守阵容 */
    public void updateLadderDefPs(LadderPsQuery query) {
        game.rpcSend().fireAndForgetCross(RpcLadderPath.UPDATE_PS_G2C, query);
    }

    /** 向跨服申请增加大神风采录像 */
    public void addRecord(LadderReportQuery query) {
        game.rpcSend().fireAndForgetCross(RpcLadderPath.ADD_RECORD_G2C, query);
    }

    /** 向跨服申请获取大神风采录像 */
    public LadderReportResult getMasterReport(int limit) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.GET_MASTER_REPORTS, limit, LadderReportResult.class);
    }

    /** 向跨服申请获取排行榜 */
    public LadderRankListResult getRankList(LadderRankListQuery query) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.GET_RANK_LIST, query, LadderRankListResult.class);
    }

    public void onAddRecordCmd(RpcMsgHead head, LadderReportQuery query) {
        String oppoRoleId = query.oppoRoleId;
        LadderReport report = query.report;
        Role role = game.role.getRole(oppoRoleId);
        if(role == null) {
            return;
        }
        role.getPvp().ladder.addReport(report);
    }


    private void reset(RpcMsgHead rpcMsgHead, String query) {
        String oppoRoleId = query;
        Role role = game.role.getRole(oppoRoleId);
        if(role == null) {
            return;
        }
        role.getPvp().ladder.clearData();
        LadderDataResp.doSend(role);
    }


    public LadderAdmireResult admire(LadderRankAdmireQuery query) {
        return game.rpcSend().requestResponseCross(RpcLadderPath.RANK_ADMIRE, query, LadderAdmireResult.class);
    }

    private OppoGuildResult getOppoGuildCmd(RpcMsgHead rpcMsgHead, LadderLockOppoQuery query) {
        OppoGuildResult result = new OppoGuildResult();
        String oppoRoleId = query.oppoId;
        Role role = game.role.getRole(oppoRoleId);
        String guildName = "";
        if(role == null) {
            result.fail("获取用户公会名失败，用户不存在");
            return result;
        }
        if (role.getRes().guild.checkGuild().ok()) {
            guildName = role.getRes().guild.guildName;
        }
        result.guildName = guildName;
        result.success();
        return result;
    }

}
